package com.hongsen.cn.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.hongsen.cn.Utils.LogUtil;
import com.hongsen.cn.bean.WorkPlanBean;
import com.hongsen.cn.bean.WorkPlanFBean;
import com.hongsen.cn.presenter.CircleProgress;
import com.hongsen.cn.base.BaseRecyclerViewHolder;
import com.hongsen.cn.dayandmonth.R;
import com.hongsen.cn.dayandmonth.databinding.ItemWorkScheduleBinding;
import com.hongsen.cn.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class WorkScheduleAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {

    private List<WorkPlanFBean> strings = new ArrayList<>();
    private Context mcontext;
    public OnItemClickListener mOnItemClickLitener;

    public WorkScheduleAdapter(Context context, List<WorkPlanFBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemWorkScheduleBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_work_schedule, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }


    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.bindData(strings.get(position));

        TourViewHolder tv = (TourViewHolder) holder;

        WorkPlanFBean fBean = strings.get(position);

        if (fBean.workOne != null) {
            tv.chartWorkOne.setVisibility(View.VISIBLE);
            tv.tvOne.setVisibility(View.VISIBLE);
            tv.chartWorkOne.setMaxValue(100);
            tv.chartWorkOne.setValue(Float.parseFloat(fBean.getWorkOne().getPercentage()) * 100);
            tv.tvOne.setText(fBean.getWorkOne().getMjxm());
        }else{
            tv.chartWorkOne.setVisibility(View.GONE);
            tv.tvOne.setVisibility(View.GONE);
        }

        if (fBean.workTwo != null) {
            tv.chartWorkTwo.setVisibility(View.VISIBLE);
            tv.tvTwo.setVisibility(View.VISIBLE);
            tv.chartWorkTwo.setMaxValue(100);
            tv.chartWorkTwo.setValue(Float.parseFloat(fBean.getWorkTwo().getPercentage()) * 100);
            tv.tvTwo.setText(fBean.workTwo.getMjxm());
        }else{
            tv.chartWorkTwo.setVisibility(View.GONE);
            tv.tvTwo.setVisibility(View.GONE);
        }

        if (fBean.workThree != null) {
            tv.chartWorkThree.setVisibility(View.VISIBLE);
            tv.tvThree.setVisibility(View.VISIBLE);
            tv.chartWorkThree.setMaxValue(100);
            tv.chartWorkThree.setValue(Float.parseFloat(fBean.getWorkThree().getPercentage()) * 100);
            tv.tvThree.setText(fBean.workThree.getMjxm());
        }else{
            tv.chartWorkThree.setVisibility(View.GONE);
            tv.tvThree.setVisibility(View.GONE);
        }


        if (fBean.workFour != null) {
            tv.chartWorkFour.setVisibility(View.VISIBLE);
            tv.tvFour.setVisibility(View.VISIBLE);
            tv.chartWorkFour.setMaxValue(100);
            tv.chartWorkFour.setValue(Float.parseFloat(fBean.getWorkFour().getPercentage()) * 100);
            tv.tvFour.setText(fBean.workFour.getMjxm());
        }else{
            tv.chartWorkFour.setVisibility(View.GONE);
            tv.tvFour.setVisibility(View.GONE);
        }

        if (fBean.workFive != null) {
            tv.chartWorkFive.setVisibility(View.VISIBLE);
            tv.tvFive.setVisibility(View.VISIBLE);
            tv.chartWorkFive.setMaxValue(100);
            tv.chartWorkFive.setValue(Float.parseFloat(fBean.getWorkFive().getPercentage()) * 100);
            tv.tvFive.setText(fBean.workFive.getMjxm());
        }else{
            tv.chartWorkFive.setVisibility(View.GONE);
            tv.tvFive.setVisibility(View.GONE);
        }


        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<WorkPlanFBean> {
        ItemWorkScheduleBinding binding;
        CircleProgress chartWorkFive, chartWorkFour, chartWorkThree, chartWorkTwo, chartWorkOne;
        TextView tvOne, tvTwo, tvThree, tvFour, tvFive;


        public TourViewHolder(ItemWorkScheduleBinding dataBinding) {
            super(dataBinding.getRoot());
            binding = dataBinding;
            chartWorkFive = dataBinding.chartWorkFive;
            chartWorkFour = dataBinding.chartWorkFour;
            chartWorkThree = dataBinding.chartWorkThree;
            chartWorkTwo = dataBinding.chartWorkTwo;
            chartWorkOne = dataBinding.chartWorkOne;

            tvOne = dataBinding.tvNameOne;
            tvTwo = dataBinding.tvNameTwo;
            tvThree = dataBinding.tvNameThree;
            tvFour = dataBinding.tvNameFour;
            tvFive = dataBinding.tvNameFive;
        }

        @Override
        public void bindData(WorkPlanFBean data) {
            binding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    //把String转化为float
    public float convertToFloat(String number, float defaultValue) {
        if (TextUtils.isEmpty(number)) {
            return defaultValue;
        }
        try {
            return Float.parseFloat(number);
        } catch (Exception e) {
            return defaultValue;
        }

    }

}
